package com.hsc.www;

import com.alibaba.fastjson.JSONObject;
import com.ctrip.framework.apollo.model.ConfigChange;
import com.ctrip.framework.apollo.model.ConfigChangeEvent;
import com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

@Component
public class PropertiesRefresher implements ApplicationContextAware {

    private ApplicationContext applicationContext;

    @ApolloConfigChangeListener()
    public void onChange(ConfigChangeEvent changeEvent) {

        List<ConfigChange> configChangeList = changeEvent.changedKeys().stream().map(changedKey -> changeEvent.getChange(changedKey)).collect(Collectors.toList());
        System.out.println("Autoq update apollo changed value successfully " + JSONObject.toJSONString(configChangeList));
//        this.applicationContext.publishEvent(new EnvironmentChangeEvent(changeEvent.changedKeys()));

    }


    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}
